<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;

class IndexController extends Controller
{
    //聊天室首页
    public function index()
    {
        $data = DB::table('ly_user')->where('user_name', Session::get('isUserName'))->join('ly_info', 'info_id', 'ly_info.uid')->first();
        //随机取一个房间
        // $room = DB::table('ly_room')->inRandomOrder()->first();
        //房间列表
        // $room = DB::table('ly_room')->join('ly_user', 'room_user', 'ly_user.user_name')->join('ly_info', 'ly_user.info_id', 'ly_info.uid')->get();
        $room = DB::table('ly_room')->join('ly_user', 'room_user', 'ly_user.user_name')->join('ly_info', 'ly_user.info_id', 'ly_info.uid')->get();
        //获取热度
        foreach ($room as $key => $value) {
            $room[$key]->hot = DB::table('ly_news')->where('room_id', $value->room_id)->count();
        }
        // dd($data);
        if ($data == "[]" || $data == null) {
            Session::flush();
            return redirect('/login.html'); //跳转首页
        } else {
            return view('index', ['data' => $data, "room" => $room, 'socket' => config('server')]);
        }
        // dd($room);
    }

    //房间编辑页面
    public function editroom(Request $request)
    {
        $id = $request->id;
        if ($id != null) {
            $data = DB::table('ly_room')->where('room_id', $id)->first();
            return view('user/editroom', ['data' => $data]);
        }
    }

    //聊天室登录页
    public function login()
    {
        return view('login');
    }

    //聊天室创建房间页
    public function creator()
    {
        $data = DB::table('ly_user')->where('user_name', Session::get('isUserName'))->join('ly_info', 'info_id', 'ly_info.uid')->first();
        return view('user/creator', ['data' => $data]);
    }

    //聊天室个人信息
    public function userinfo()
    {
        $data = DB::table('ly_user')->where('user_name', Session::get('isUserName'))->join('ly_info', 'info_id', 'ly_info.uid')->first();
        $info = config('webset');
        $tuiguang = 'http://' . $info['weburl'] . '/register?pid=' . $data->user_name;
        $lvexp = DB::table('ly_lv')->where('sign', $data->lv + 1)->first();

        // dd($lvexp);
        // dd($data);
        return view('user/userinfo', ['data' => $data, 'tuiguang' => $tuiguang, 'lvexp' => $lvexp]);
    }

    //信息查询
    public function getinfo(Request $request)
    {
        $name = $request->name;
        $room = $request->room;

        $data = DB::table('ly_user')->where('user_name', $name)->join('ly_info', 'info_id', 'ly_info.uid')->first();
        $jy = DB::table('ly_punish')->where('user_id', $name)->where('result', 1)->get();
        $hmd = DB::table('ly_punish')->where('user_id', $name)->where('result', 2)->get();
        if ($hmd == '[]') {
            $hmd = false;
        }
        if ($jy == '[]') {
            $jy = false;
        }
        $info = [
            'name' => $name,
            'room' => $room,
            'jy' => $jy,
            'hmd' => $hmd
        ];
        if ($data == null) {
            return view('user/getinfo');
            # code...
        } else {
            return view('user/getinfo', ['data' => $data, 'info' => $info]);
        }
    }

    //签到页
    public function signin(Request $request)
    {
        $name = $request->name;
        if ($name != null) {
            $data = DB::table('ly_signin')->where('user_id', $name)->get();

            if ($data == '[]' || $data == null) {
                return view('user/signin');
            } else {
                foreach ($data as $key => $value) {
                    $qd[$key] = date('j', strtotime($value->time)) - 1;
                }
                return view('user/signin', ["data" => $data, 'qdarr' => implode(",", $qd)]);
            }
        }
    }

    //签到历史
    public function historysignin(Request $request)
    {
        $name = $request->name;
        if ($name != null) {
            $data = DB::table('ly_signin')->where('user_id', $name)->get();
            $lj = 0;
            $by = 0;
            if ($data == '[]' || $data == null) {
                return response($this->_tips(204, '无数据'));
            } else {
                $date = date("Y-m");
                foreach ($data as $key => $value) {
                    $lj = $lj + $value->get;
                    if ($date == date("Y-m", strtotime($value->time))) {
                        $by = $by + 1;
                    }
                    $info['jl'][$key] = [
                        'time' => $value->time,
                        'jl' => $value->get,
                    ];
                }

                $info['lj'] = $lj;
                $info['zj'] = count($data);;
                $info['by'] = $by;
                return response($this->_tips(200, '获取成功', $info));
            }
        } else {
            return response($this->_tips(204, '格式错误'));
        }
    }
}
